﻿namespace MonthYearPickerApp.Views
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Controls;
    using System.Globalization;

    using Microsoft.Phone.Controls.Primitives;
    using WP7Contrib.View.Controls.DateTimePicker;

    /// <summary>
    /// Month year picker page
    /// </summary>
    public partial class MonthYearPicker
    {

        // Methods
        public MonthYearPicker()
        {
            this.InitializeComponent();
            this.PrimarySelector.DataSource = new YearDataSource();
            this.SecondarySelector.DataSource = new MonthDataSource();
            this.TertiarySelector.DataSource = new DayDataSource();

            ((DataSource)this.PrimarySelector.DataSource).AltSelectionChanged += this.HandleAltDataSourceSelectionChanged;
            ((DataSource)this.SecondarySelector.DataSource).AltSelectionChanged += this.HandleAltDataSourceSelectionChanged;
            ((DataSource)this.TertiarySelector.DataSource).AltSelectionChanged += this.HandleAltDataSourceSelectionChanged;

            this.InitializeDateTimePickerPage(this.PrimarySelector, this.SecondarySelector, this.TertiarySelector);
        }

        /// <summary>
        /// Handles the alt data source selection changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void HandleAltDataSourceSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataSource source = (DataSource)sender;
            if (source is YearDataSource)
            {
                // Set the year part of the value
                this.PrimarySelector.DataSource.SelectedItem = source.SelectedItem;
                if (source.SelectedItem != null && this.Value.HasValue)
                {
                    this.Value = new DateTime(
                        ((DateTimeWrapper)source.SelectedItem).DateTime.Year, this.Value.Value.Month, 1);
                }
            }
            else if (source is MonthDataSource)
            {
                // Set the month part of the value
                this.SecondarySelector.DataSource.SelectedItem = source.SelectedItem;
                if (source.SelectedItem != null && this.Value.HasValue)
                {
                    // Get the last day of the month
                    this.Value = new DateTime(
                        this.Value.Value.Year, ((DateTimeWrapper)source.SelectedItem).DateTime.Month, 1);
                }
            }
            else if (source is DayDataSource)
            {
                // We don't care about the day
                this.TertiarySelector.DataSource.SelectedItem = source.SelectedItem;
            }                       
        }

        /// <summary>
        /// Gets a sequence of LoopingSelector parts ordered according to culture string for date/time formatting.
        /// </summary>
        /// <returns>
        /// LoopingSelectors ordered by culture-specific priority.
        /// </returns>
        protected override IEnumerable<LoopingSelector> GetSelectorsOrderedByCulturePattern()
        {
            return GetSelectorsOrderedByCulturePattern(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToUpperInvariant(), new char[] { 'Y', 'M', 'D' }, new LoopingSelector[] { this.PrimarySelector, this.SecondarySelector, this.TertiarySelector });
        }
    }
}